package com.kexio.config.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.kexio.config.service.ConfigService;
import com.kexio.config.service.impl.ConfigServiceImpl;

/**
 * 配置管理自动配置
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@Configuration
@ConditionalOnProperty(prefix = "kexio.config", name = "enabled", havingValue = "true")
@EnableConfigurationProperties(ConfigProperties.class)
@ComponentScan(basePackages = "com.kexio.config")
public class ConfigAutoConfiguration {
    
    private static final Logger logger = LoggerFactory.getLogger(ConfigAutoConfiguration.class);
    
    /**
     * 配置服务Bean
     */
    @Bean
    @ConditionalOnMissingBean
    public ConfigService configService() {
        logger.info("注册配置服务: ConfigServiceImpl");
        return new ConfigServiceImpl();
    }
    
    /**
     * 配置管理启动日志
     */
    @Bean
    public ConfigStartupLogger configStartupLogger(ConfigProperties properties) {
        return new ConfigStartupLogger(properties);
    }
    
    /**
     * 启动日志组件
     */
    public static class ConfigStartupLogger {
        
        private final ConfigProperties properties;
        
        public ConfigStartupLogger(ConfigProperties properties) {
            this.properties = properties;
            logStartupInfo();
        }
        
        private void logStartupInfo() {
            logger.info("========================================");
            logger.info("⚙️ Kexio Config Service 启动成功!");
            logger.info("🔧 基础配置:");
            logger.info("   配置管理: ✅ 启用");
            logger.info("   字典管理: {}", properties.getDict().isEnabled() ? "✅ 启用" : "❌ 禁用");
            logger.info("   动态配置: {}", properties.isDynamicConfig() ? "✅ 启用" : "❌ 禁用");
            logger.info("   配置版本控制: {}", properties.isVersionControl() ? "✅ 启用" : "❌ 禁用");
            
            logger.info("📚 字典配置:");
            if (properties.getDict().isEnabled()) {
                logger.info("   缓存过期: {}分钟", properties.getDict().getCacheExpireMinutes());
                logger.info("   预加载: {}", properties.getDict().isPreload() ? "✅ 启用" : "❌ 禁用");
                logger.info("   层级支持: {}", properties.getDict().isHierarchical() ? "✅ 启用" : "❌ 禁用");
            }
            
            logger.info("🔄 缓存配置:");
            logger.info("   本地缓存: {}", properties.getCache().isLocalEnabled() ? "✅ 启用" : "❌ 禁用");
            logger.info("   Redis缓存: {}", properties.getCache().isRedisEnabled() ? "✅ 启用" : "❌ 禁用");
            if (properties.getCache().isLocalEnabled()) {
                logger.info("   本地缓存大小: {}", properties.getCache().getLocalMaxSize());
                logger.info("   本地缓存过期: {}分钟", properties.getCache().getLocalExpireMinutes());
            }
            if (properties.getCache().isRedisEnabled()) {
                logger.info("   Redis缓存过期: {}分钟", properties.getCache().getRedisExpireMinutes());
            }
            
            logger.info("🔐 安全配置:");
            logger.info("   敏感配置加密: {}", properties.getSecurity().isEncryptSensitive() ? "✅ 启用" : "❌ 禁用");
            logger.info("   配置修改审计: {}", properties.getSecurity().isAuditChanges() ? "✅ 启用" : "❌ 禁用");
            logger.info("   权限验证: {}", properties.getSecurity().isRequirePermission() ? "✅ 启用" : "❌ 禁用");
            
            logger.info("📊 监控配置:");
            logger.info("   性能监控: {}", properties.getMonitor().isEnabled() ? "✅ 启用" : "❌ 禁用");
            if (properties.getMonitor().isEnabled()) {
                logger.info("   慢查询阈值: {}毫秒", properties.getMonitor().getSlowQueryThreshold());
                logger.info("   统计窗口: {}分钟", properties.getMonitor().getStatsWindow());
            }
            
            logger.info("🚀 配置管理服务已就绪!");
            logger.info("========================================");
        }
    }
}
